package cn.jy.nvr.utils;

import java.io.IOException;
import java.net.URI;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * httpclient公共方法
 *    发送get请求
 *    发送post请求
 * @author Administrator
 *
 */
public class HttpClientUtil {

	/**
	 * 发送一个get请求到对应的url并获取json返回值
	 * @param url 请求的url
	 * @return json返回值
	 */
	public static String httpGet(String url) {
		try {
			//创建一个执行对象
			HttpClient execution = HttpClientBuilder.create().build();
			//创建get请求
			HttpGet httpGet  = new HttpGet(url);
			//通过执行对象执行get请求并获取响应对象
			HttpResponse response = execution.execute(httpGet);
			System.out.println("statusCode:"+response.getStatusLine().getStatusCode());
			//从响应对象的响应实体中获取json返回值
			return EntityUtils.toString(response.getEntity());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	 public static String doGet(String url, Map<String, String> param) {

	        // 创建Httpclient对象
	        CloseableHttpClient httpclient = HttpClients.createDefault();

	        String resultString = "";
	        CloseableHttpResponse response = null;
	        try {
	            // 创建uri
	            URIBuilder builder = new URIBuilder(url);
	            if (param != null) {
	                for (String key : param.keySet()) {
	                    builder.addParameter(key, param.get(key));
	                }
	            }
	            URI uri = builder.build();

	            // 创建http GET请求
	            HttpGet httpGet = new HttpGet(uri);

	            // 执行请求
	            response = httpclient.execute(httpGet);
	            // 判断返回状态是否为200
	            if (response.getStatusLine().getStatusCode() == 200) {
	                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                if (response != null) {
	                    response.close();
	                }
	                httpclient.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	        return resultString;
	    }


	/**
	 * 发送post请求到url并且携带menuJsonStr的参数
	 * @param url post请求的url
	 * @param menuJsonStr json菜单
	 * @return json返回值
	 */
	public static String httpPost(String url, String menuJsonStr) {
		try {
			//创建一个执行对象
			HttpClient execution = HttpClientBuilder.create().build();
			//创建Post请求
			HttpPost httpPost = new HttpPost(url);
			//在请求上面设置参数 setEntity
			httpPost.setEntity(new StringEntity(menuJsonStr, "utf-8"));
			//通过执行对象执行get请求并获取响应对象
			HttpResponse response = execution.execute(httpPost);
			System.out.println("statusCode:"+response.getStatusLine().getStatusCode());
			//从响应对象的响应实体中获取json返回值
			return EntityUtils.toString(response.getEntity());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	

}
